//
//  ImageAsset.m
//  OntheRoadV4
//
//  Created by ZhangYanhua on 14/12/1.
//  Copyright (c) 2014年 EricHuang. All rights reserved.
//

#import "ImageAsset.h"
#import "NSDate-Utilities.h"
#import "ALAssetsGroupEnumerator.h"
@interface ImageAsset()

@property (nonatomic, strong) PHFetchResult *assetsFetchResult;
@end

@implementation ImageAsset

- (id)initWithALAsset:(ALAsset *)asset
{
    self = [super init];
    if (self) {
        _asset = (id<DownscalingProtol>)asset;
        _assetDate = [asset valueForProperty:ALAssetPropertyDate];
        _assetIdentifier = [asset valueForProperty:ALAssetPropertyAssetURL];
        
        NSString *assetType = [asset valueForProperty:ALAssetPropertyType];
        _isPhoto = [assetType isEqualToString:ALAssetTypePhoto];
        
        _duration = [[asset valueForProperty:ALAssetPropertyDuration] doubleValue];
    }
    return self;
}

- (id)initWithPHFetchResult:(PHFetchResult *)assetsFetchResult
                    isPhoto:(BOOL)isPhoto
                 assetIndex:(NSInteger)index
{
    self = [super init];
    if (self) {
        // 不retain PHAsset
        _assetsFetchResult = assetsFetchResult;
        _index = index;
        
        _isPhoto = isPhoto;
    }
    return self;
}

- (id)asset
{
    if (_asset) {
        return _asset;
    } else if (_assetsFetchResult && _index >= 0 && _index < _assetsFetchResult.count) {
        return _assetsFetchResult[_index];
    }
    
    return nil;
}
- (NSDate *)assetDate
{
    if (_assetsFetchResult) {
        _assetDate = [(PHAsset *)self.asset creationDate];
    }
    
    return _assetDate;
}

- (NSString *)assetIdentifier
{
    if (_assetsFetchResult) {
        _assetIdentifier = [(PHAsset *)self.asset localIdentifier];
    }
    
    return _assetIdentifier;
}
- (double)duration
{
    if (_assetsFetchResult) {
        _duration = [(PHAsset *)self.asset duration];
    }
    
    return _duration;
}
+ (void)deletedAssetsOfArray:(NSMutableArray *)array inGroup:(NSString *)groupIdentifier
{
    NSArray *tempArray = [array copy];
    for (ImageAsset *asset in tempArray) {
        if (asset.groupIdentifier && [asset.groupIdentifier isEqualToString:groupIdentifier]) {
            [array removeObject:asset];
        }
    }
}



- (BOOL)isInTheSameSectionWithAsset:(ImageAsset *)imgAsset
              tenDaysAfterFirstDate:(NSDate *)tenDaysDate
{
    if (imgAsset == nil) {
        return YES;
    }
    
//    BOOL isSameDay = [QWImageMangerConfig isSameDayWithDate1:self.assetDate Date2:imgAsset.assetDate];
    
    BOOL isSameDay = [self.assetDate isEqualToDateIgnoringTime:imgAsset.assetDate];

//    if (isSameDay) {
//        return YES;
//    } else {
//        if ([imgAsset.assetDate isLaterThanDate:tenDaysDate] ||
//            [imgAsset.assetDate isSameMonthAsDate:tenDaysDate]) {
//            return NO;
//        } else if ([_assetDate isSameMonthAsDate:imgAsset.assetDate]) {
//            return YES;
//        }
//    }
//    
    return isSameDay;
}

- (NSUInteger)hash
{
    return [self.assetIdentifier hash];
}
- (BOOL)isEqual:(id)object
{
    if (![super isEqual:object]) {
        if ([object isKindOfClass:[ImageAsset class]]) {
            if ([self.assetIdentifier isKindOfClass:[NSString class]])
            {
                return [self.assetIdentifier isEqualToString:[object assetIdentifier]];
            }else if ([self.assetIdentifier isKindOfClass:[NSURL class]])
            {
                NSString  *firstStr = [(NSURL*)self.assetIdentifier absoluteString];
                NSString  *secondStr = [(NSURL*)[object assetIdentifier] absoluteString];
                 return [firstStr isEqualToString:secondStr];
            }
           
        }
        
        return NO;
    }
    
    return YES;
}

- (NSString *)durationString
{
    if (!_durationString) {
        if (self.isPhoto) {
            _durationString = @"";
        } else {
            int mins = ((int)self.duration / 60);
            int second = ((int)self.duration % 60);
            
            _durationString = [NSString stringWithFormat:@"%d:%02d", mins, second];
        }
    }
    
    return _durationString;
}
- (void)getFullScreenImageWithBlock:(FetchedImageHandler)handler
{
    if ([self.asset isKindOfClass:[ALAsset class]]) {
        UIImage *image = [UIImage imageWithCGImage:[[(ALAsset*)self.asset defaultRepresentation] fullScreenImage]];
        if (handler) handler(image, nil);
    } else {
        PHCachingImageManager *imageManager = [[ALAssetsGroupEnumerator sharedInstance] imageManager];
        [imageManager requestImageForAsset:(PHAsset*)self.asset
                                targetSize:[UIScreen mainScreen].bounds.size
                               contentMode:PHImageContentModeAspectFill
                                   options:nil
                             resultHandler:^(UIImage *result, NSDictionary *info) {
                                 if (handler) handler(result, info);
                             }];
    }
}

- (void)getThumbnailImageWithSize:(CGSize)size resultHandler:(FetchedImageHandler)handler
{
    if ([self.asset isKindOfClass:[ALAsset class]]) {
        UIImage *image = [self.asset minThumbnail];
        if (handler) handler(image, nil);
    } else {
        PHCachingImageManager *imageManager = [[ALAssetsGroupEnumerator sharedInstance] imageManager];
        [imageManager requestImageForAsset:(PHAsset*)self.asset
                                targetSize:size
                               contentMode:PHImageContentModeAspectFill
                                   options:nil
                             resultHandler:^(UIImage *result, NSDictionary *info) {
                                 if (handler) handler(result, info);
                             }];
    }
}

- (void)getVideoAssetWithBlock:(FetchedVideoHandler)handler
{
    if ([self.asset isKindOfClass:[ALAsset class]]) {
        AVAsset *avAsset = [AVAsset assetWithURL:[[(ALAsset*)self.asset defaultRepresentation] url]];
        if (handler) handler(avAsset, nil);
    } else {
        PHCachingImageManager *imageManager = [[ALAssetsGroupEnumerator sharedInstance] imageManager];
        [imageManager requestAVAssetForVideo:(PHAsset*)self.asset
                                     options:nil
                               resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
                                   if (handler) handler(asset, info);
                               }];
    }
}


@end
